﻿/**
 * @Description: 级联下拉框公共组件
 * @author: xiongchengming
 * @date: 2015年5月7日 下午5:59:56
 * 
 */
var xSelect = function(param) {
	this.data = param["data"];// 下拉框数据 json数组数据 格式// [{namekey:"四川",valuekey:"100000","children":[{},{}]},{}]
	this.divid = param["divid"];// 渲染目标div id
	this.selectnames=param["selectnames"];//下拉框对应的字段名称
	this.disabled=param["disabled"];
	this.parentdiv = $("#" + this.divid);
	this.initvalue = param["initvalue"]; // 初始化下拉框 值 格式 "四川，成都，锦江区"
	this.datarecord = {};
	this.selectobjs = {};
	this.changecallback = param["changecallback"];//下拉框选中回调
	this.selectvalue = new Array();
	this.selecttext = new Array();
	this.namekey=param["namekey"];//用于下拉框显示下拉选项的 text字段名称。
	this.valuekey=param["valuekey"];//用于下拉框显示下拉选项的 的value字段名称
	this.width=param["width"];
	this.height=param["height"];
	if(this.namekey==null||this.namekey==undefined)
		this.namekey="name";
	if(this.valuekey==null||this.valuekey==undefined)
		this.valuekey="value";
	this.selectlabels=param["selectlabels"];//{"1":"省","2":"市"}
}
xSelect.prototype = {
	getSelectValue : function() {
		return this.selectvalue;
	},
	getValue : function() {
		return this.selectvalue;
	},
	getSelectText : function() {
		return this.selecttext;
	},
	getText : function() {
		return this.selecttext;
	},
	getdata : function() {
		this.cursedata(this.data, 1);
	},
	//值为字符串，如果是级联下拉框  每一个下拉框的值用逗号分隔，
	setValue:function(valuestrs){
		var values=null;
		if(valuestrs!=undefined&&valuestrs!=null)
			values=valuestrs.split(",");
		else
			values=[];
		this.selectvalue=values;
		for(var i=0;i<values.length;i++){
			var value=values[i];
			var selectobj=$("#select-"+this.divid+"-"+(i+1));
			var optobjs=selectobj.children("option");
			for(var j=0;j<optobjs.length;j++){
				var optobj=$(optobjs[i]);
				var optval=optobj.attr("value");
				if($.trim(optval)==$.trim(value))
					optobj.attr("selected", true);
				else
					optobj.attr("selected", false);
			}
		}
	},
	//值为字符串，如果是级联下拉框  每一个下拉框的值用逗号分隔，如  false,false,false
	setDisabled:function(boolstrs){
		var values=null;
		if(boolstrs!=undefined&&boolstrs!=null)
			values=boolstrs.split(",");
		else
			values="false";
		for(var i=0;i<values.length;i++){
			var value=$.trim(values[i]);
			var selectobj=$("#select-"+this.divid+"-"+(i+1));
			if(value=="true"){
				selectobj.css("pointer-events","none");
				selectobj.attr("disabled","disabled");
			}else{
				selectobj.css("pointer-events","auto");
				selectobj.removeAttr("disabled","disabled");
			}
		}
	},
	cursedata : function(jdata, level) {
		if (this.datarecord[level] == null
				|| this.datarecord[level] == undefined) {
			this.datarecord[level] = {};
		}
		for (var i = 0; i < jdata.length; i++) {
			var reg = jdata[i];
			this.datarecord[level][reg[this.valuekey]] = reg;
			if (reg["children"] != null && reg["children"] != undefined
					&& reg["children"].length > 0) {
				this.cursedata(reg["children"], level + 1);
			} else {
				continue;
			}
		}
	},
	render : function() {
		this.getdata();
		creatselect(this.data, this.parentdiv, this.selectvalue,
				this.selecttext, this.datarecord, 1, this.initvalue,this.namekey,this.valuekey,
				this.changecallback,this.selectlabels,this.selectnames,this.width,this.height,this.disabled);
	}
}
var creatselect = function(jdatas, parentdiv, selectvalue, selecttext,
		datarecord, level, initkeysStr,namekey,valuekey,changecallback,
		selectlabels,selectnames,width,height,disabled) {
	var parentdivid=parentdiv.attr("id");
	// 删除div
	for (var j = level; j < 5; j++) {
		var lem = $("#xselect-"+parentdivid+"-" + j);
		if (lem != null && lem != undefined)
			lem.remove();
	}
	while(selectvalue.length>(level-1)){
		selectvalue.splice(selectvalue.length-1, 1);
		selecttext.splice(selectvalue.length-1, 1);
	}
	if (jdatas == null ||jdatas == undefined||jdatas.length==0){
		return;
	}
	var initkeys = null;
	var initkey=null;
	if (initkeysStr != null) {
		initkeysStr=initkeysStr+"";
		   initkeys = initkeysStr.split(",");
		   initkey = initkeys[level - 1];
	}
	if (jdatas.length > 0) {
		var selectdiv = $('<div id="xselect-'+parentdivid+"-"+  level
				+ '" style="float:left;margin-right:2px;"></div>');
		parentdiv.append(selectdiv);
		var selectname=null;
		if(selectnames!=null&&selectnames!=undefined){
		  var snarr=selectnames.split(",");
		  selectname=snarr[level-1];
	    }
		var width1=160;
		if(width!=null&&width!=undefined)
			width1=width;
		var height1=30;
		if(height!=null&&height!=undefined)
			height1=height;
	    var selectobj=null;
		if(selectname!=undefined &&selectname!=null){
			selectobj = $('<select id="select-' +parentdivid+"-"+ level
					+ '"  name="'+selectname+'" style="width:'+width1+'px;"  class="input-sm inline"></select>');
		}else{
			selectobj = $('<select id="select-' +parentdivid+"-"+ level
					+ '" style="width:'+width1+'px;" class="input-sm inline"></select>');
		}
		selectdiv.append(selectobj);
		if(disabled!=undefined&&disabled!=null)
			selectobj.css("pointer-events","none");
		// 选择改变
		selectobj.change(function() {
			var selectkey = selectobj.children('option:selected').val();
			var selecttext1 = selectobj.children('option:selected').text();
			var leveldata = datarecord[level];
			var pdata = leveldata[selectkey];
			selectvalue[level - 1]=selectkey;
			selecttext[level  - 1]=selecttext1;
				creatselect(pdata["children"], parentdiv, selectvalue,
						selecttext, datarecord, level + 1, null,namekey,valuekey,changecallback,selectlabels,
						selectnames,width,height,disabled);
				if(changecallback!=null&&changecallback!=undefined){
					
					changecallback();
				}
				
		});
		var pdata = null;
		if (initkey == null || initkey == undefined||$.trim(initkey)=="")
			initkey = jdatas[0][valuekey];
		for (var i = 0; i < jdatas.length; i++) {
			var reg = jdatas[i];
			var opname = reg[namekey];
			var opvalue = reg[valuekey];
			var optionobj = null;
			if (opvalue == initkey) {
				optionobj = $('<option value="' + opvalue + '">' + opname
						+ '</option>');
				optionobj.attr("selected", true);
				selectvalue[level - 1] = opvalue;
				selecttext[level - 1] = opname;
				pdata = reg;
			} else {
				optionobj = $('<option value="' + opvalue + '">' + opname
						+ '</option>');
			}
			selectobj.append(optionobj);
		}
		var pspan = null;
		var selectlb="";
		if(selectlabels!=null&&selectlabels!=undefined){
			if(selectlabels[""+level]!=null&&selectlabels[""+level]!=undefined)
				selectlb=selectlabels[""+level];
		}
		if(selectlb!=null&&selectlb!=undefined&&selectlb!=""){
		  pspan = $('<span style="float:left;margin-top:5px;height:25px;">&nbsp;'+selectlb+'&nbsp;&nbsp;</span>');
		  selectdiv.append(pspan);
		}
		// 下一级
		try{
		if (pdata["children"] != null && pdata["children"] != undefined) {
			creatselect(pdata["children"], parentdiv, selectvalue, selecttext,
					datarecord, level + 1, initkeysStr,namekey,valuekey,changecallback,selectlabels,
					selectnames,width,height,disabled);
		}
		}catch(e){}
		
	}
}
